package moe.fuqiuluo

import com.tencent.mobileqq.dt.model.FEBound
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import moe.fuqiuluo.comm.QSignConfig
import java.io.File

lateinit var CONFIG: QSignConfig
lateinit var BASE_PATH: File
lateinit var QSEC_RAW: ByteArray
lateinit var FEKIT_RAW: ByteArray


fun protocolInit(version: String) {
    File("txlib").also {
        if (!it.exists() || !it.isDirectory){
            it.mkdir()
        }
    }
    var path = "txlib/"+version;
    File(path).also {
        BASE_PATH = it
        if (!it.exists() || !it.isDirectory){
            it.mkdir()
        }
    }

    val config: String = "/txlib/"+version+"/config.json"
    val dtconfig: String = "/txlib/"+version+"/dtconfig.json"
    val libfekit: String = "/txlib/"+version+"/libfekit.so"
    val libQSec: String = "/txlib/"+version+"/libQSec.so"

    val json = Json { ignoreUnknownKeys = true }
    CONFIG = json.decodeFromString(getResourceAsText(config))
    FEBound.initAssertConfig(getResourceAsText(dtconfig))

    QSEC_RAW = getResourceAsBytes(libQSec)
    FEKIT_RAW = getResourceAsBytes(libfekit)

}

fun getResourceAsText(path: String): String {
    return object {}::class.java.getResource(path).readText()
}

fun getResourceAsBytes(path: String): ByteArray {
    return object {}::class.java.getResource(path).readBytes();
}
